﻿using System;
using Tilver.Coder.Core;
using Tilver.Coder.Utility;

namespace Tilver.Coder.Engine
{
    public class EngineFactory : IEngineFactory
    {
        #region IEngineFactory 成员

        public ITableEngine GetTableEngine()
        {
            return new TableEngine();
        }

        public ITableEngineEO GetTableEngineEO()
        {
            return new TableEOEngine();
        }

        public ITableEngineMO GetTableEngineMO()
        {
            return new TableMOEngine();
        }

        public IViewEngine GetViewEngine()
        {
            return new ViewEngine();
        }

        public IViewEngineEO GetViewEngineEO()
        {
            return new ViewEOEngine();
        }

        public IViewEngineMO GetViewEngineMO()
        {
            return new ViewMOEngine();
        }

        public ICommandEngine GetCommandEngine()
        {
            return new CommandEngine();
        }

        #endregion

        /// <summary>
        /// 获取数据类型的代码生成引擎
        /// </summary>
        /// <param name="tabular"></param>
        /// <returns></returns>
        public IEngine GetEngine(CoderTabular tabular)
        {
            IEngine result;
            switch (tabular.TabularType)
            {
                case TabularTypeEnum.Table:
                    ITableEngine tableEngine = GetTableEngine();
                    tableEngine.Table = (CoderTable)tabular;
                    result = tableEngine;
                    break;
                case TabularTypeEnum.View:
                    IViewEngine viewEngine = GetViewEngine();
                    viewEngine.View = (CoderView)tabular;
                    result = viewEngine;
                    break;
                case TabularTypeEnum.Command:
                    ICommandEngine commandEngine = GetCommandEngine();
                    commandEngine.Command = (CoderCommand)tabular;
                    result = commandEngine;
                    break;
                default:
                    throw new InvalidOperationException();
            }
            //result.NameSpace = tabular.DataBase.Project.NameSpace + "." + tabular.NameSpace;
            result.NameSpace = tabular.DataBase.NameSpace ;
            result.DBTextStyle = DBTextStyleUtil.GetDBTextStyle(tabular.DataBase.DataProviderType);
            return result;
        }
    }
}
